﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace jQueryDotNetLib.Controls
{
    [ParseChildren(typeof(AccordionPanel))]
    [ToolboxData("<{0}:AccordionControl runat=server></{0}:AccordionControl>")]
    public class AccordionControl : System.Web.UI.WebControls.Panel
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                if (ViewState["Text"] == null) return "";
                return (string)ViewState["Text"];
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Active
        {
            get
            {
                if (ViewState["Active"] == null) return "";
                return (string)ViewState["Active"];
            }

            set
            {
                ViewState["Active"] = value;
            }
        }            

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Animated
        {
            get
            {
                if (ViewState["Active"] == null) return "false";
                return (string)ViewState["Animated"];
            }

            set
            {
                ViewState["Animated"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool AutoHeight
        {
            get
            {
                if (ViewState["AutoHeight"] == null) return true;
                return (bool)ViewState["AutoHeight"];
            }

            set
            {
                ViewState["AutoHeight"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool ClearStyle
        {
            get
            {
                if (ViewState["ClearStyle"] == null) return false;
                return (bool)ViewState["ClearStyle"];
            }

            set
            {
                ViewState["ClearStyle"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Collapsible
        {
            get
            {
                if (ViewState["Collapsible"] == null) return false;
                return (bool)ViewState["Collapsible"];
            }

            set
            {
                ViewState["Collapsible"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Event
        {
            get
            {
                if (ViewState["Event"] == null) return "click";
                return (string)ViewState["Event"];
            }

            set
            {
                ViewState["Event"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool FillSpace
        {
            get
            {
                if (ViewState["FillSpace"] == null) return false;
                return (bool)ViewState["FillSpace"];
            }

            set
            {
                ViewState["FillSpace"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Header
        {
            get
            {
                if (ViewState["Header"] == null) return "";
                return (string)ViewState["Header"];
            }

            set
            {
                ViewState["Header"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Icons
        {
            get
            {
                if (ViewState["Icons"] == null) return "";
                return (string)ViewState["Icons"];
            }

            set
            {
                ViewState["Icons"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public bool Navigation
        {
            get
            {
                if (ViewState["Navigation"] == null) return false;
                return (bool)ViewState["Navigation"];
            }

            set
            {
                ViewState["Navigation"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string NavigationFilter
        {
            get
            {
                if (ViewState["NavigationFilter"] == null) return "";
                return (string)ViewState["NavigationFilter"];
            }

            set
            {
                ViewState["NavigationFilter"] = value;
            }
        }











        [Bindable(true)]
        [Category("Client Events")]
        [DefaultValue("")]
        [Localizable(true)]
        public string OnClientTabChange
        {
            get
            {
                if (ViewState["OnClientTabChange"] == null) return "";
                return (string)ViewState["OnClientTabChange"];
            }

            set
            {
                ViewState["OnClientTabChange"] = value;
            }
        }
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            writer.Write("<script type=\"text/javascript\">");
            writer.Write("jQuery(document).ready(function($) { var ctl=$(\"#" + this.ClientID + "\"); ctl.accordion( {");

            bool needsComma = false;

            if (NavigationFilter != "")
            {
                if (needsComma) writer.Write(",");
                writer.Write("navigationFilter: " + NavigationFilter);
                needsComma = true;
            }
            if (Navigation)
            {
                if (needsComma) writer.Write(",");
                writer.Write("navigation: true");
                needsComma = true;
            }
            if (Icons != "")
            {
                if (needsComma) writer.Write(",");
                writer.Write("icons: " + Icons);
                needsComma = true;
            }
            if (Header != "")
            {
                if (needsComma) writer.Write(",");
                writer.Write("header: " + Header);
                needsComma = true;
            }
            if (FillSpace)
            {
                if (needsComma) writer.Write(",");
                writer.Write("fillSpace: true");
                needsComma = true;
            }
            if (Event != "click")
            {
                if (needsComma) writer.Write(",");
                writer.Write("event: '" + Event + "'");
                needsComma = true;
            }
            if (Collapsible)
            {
                if (needsComma) writer.Write(",");
                writer.Write("collapsible: true");
                needsComma = true;
            }
            if (ClearStyle)
            {
                if (needsComma) writer.Write(",");
                writer.Write("clearStyle: true");
                needsComma = true;
            }
            if (!AutoHeight)
            {
                if (needsComma) writer.Write(",");
                writer.Write("autoHeight: false");
                needsComma = true;
            }
            if (Animated!="false")
            {
                if (needsComma) writer.Write(",");
                if (Animated == System.Boolean.TrueString || Animated == System.Boolean.FalseString)
                {
                    writer.Write("animated: " + Animated.ToString().ToLower());
                }
                else
                {
                    writer.Write("animated: '" + Animated + "'");
                }
                needsComma = true;
            }
            if (Active!="")
            {
                if (needsComma) writer.Write(",");
                writer.Write("active: " + Active);
                needsComma = true;
            }


            writer.Write("} );");
            if (OnClientTabChange != "") writer.Write("ctl.bind('accordionchange', function(event, ui) {" + OnClientTabChange + "});");
            writer.Write("});");
            writer.Write("</script>");
        }
    }
}